<template>
  <div class="shareDetail">
    <van-nav-bar @click-left="$router.back()" class="login_head">
      <template #left>
        <span class="iconfont arrowleft">&#xe637;</span>
      </template>
    </van-nav-bar>
    <div class="head" v-if="myarticle.author">
      <h3>{{ myarticle.title }}</h3>
      <div class="info">
        <img :src="baseUrl + myarticle.author.avatar" alt="" />
        <div class="right">
          <div class="name">{{ myarticle.author.nickname }}</div>
          <div class="time">{{ myarticle.created_at | formatTime }}</div>
        </div>
      </div>
    </div>

    <div class="content" v-html="myarticle.content"></div>
    <!-- 评论区域 底部固定 -->
    <div class="comment">
      <div class="left">
        <van-field
          v-model="value"
          placeholder="我来补充两句"
          disabled
          @click="tocom"
        />
      </div>
      <div class="right">
        <div class="item">
          <div><van-icon name="star-o" /></div>
          <div>{{ myarticle.share }}</div>
        </div>
        <div class="item">
          <div><van-icon name="good-job-o" /></div>
          <div>{{ myarticle.star }}</div>
        </div>
        <div class="item">
          <div><van-icon name="records" /></div>
          <div>{{ myarticle.read }}</div>
        </div>
      </div>
    </div>
    <!-- 评论 -->
    <div class="commentarea">
      <van-cell>
        <template #title>
          <div class="comtitle">
            评论
          </div>
        </template>
      </van-cell>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <commentItem :comList="comList"></commentItem>
      </van-list>
    </div>

    <!-- 评论弹出 -->
    <div class="commentPop">
      <van-popup v-model="isShow" position="bottom" :style="{ height: '30%' }">
        <textarea
          class="textarea"
          placeholder="我来补充两句"
          ref="text"
        ></textarea>
        <div class="senc" @click="send">发送</div>
      </van-popup>
    </div>
  </div>
</template>

<script>
import {
  articlesShareDetail,
  articleComments,
  publishComment
} from '@/api/api.js'
import commentItem from './commentItem.vue'
export default {
  components: { commentItem },
  data () {
    return {
      baseUrl: process.env.VUE_APP_URL,
      id: '',
      myarticle: {},
      value: '',
      isShow: false,
      comList: [],
      loading: false,
      finished: false,
      start: 0
    }
  },
  created () {
    this.id = this.$route.params.id
    this.getArt()
  },

  methods: {
    // 发送评论
    async send () {
      const res = await publishComment({
        content: this.$refs.text.value,
        article: this.id
      })
      this.comList.unshift(res.data.data)
      this.$refs.text.value = ''
      this.isShow = false
    },
    // 获取评论
    async onLoad () {
      this.start++
      const res = await articleComments({
        id: this.id,
        limit: 5,
        start: this.start
      })
      this.comList = [...this.comList, ...res.data.data.list]
      this.loading = false
      if (res.data.data.list.length === 0) {
        this.finished = true
      }
    },

    // 评论
    tocom () {
      this.isShow = true
    },
    async getArt () {
      const res = await articlesShareDetail(this.id)
      this.myarticle = res.data.data
    }
  }
}
</script>

<style lang="less" scoped>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
.shareDetail {
  margin-bottom: 77px;
  padding: 10px;
  .arrowleft {
    font-size: 50px;
    margin-left: -20px;
  }
  .head {
    margin-top: 10px;
    h3 {
      font-weight: normal;
      font-size: 18px;
      margin-bottom: 20px;
    }
    .info {
      display: flex;
      margin-bottom: 40px;
      img {
        width: 32px;
        height: 32px;
        border-radius: 50%;
        margin-right: 10px;
      }
      .right {
        font-size: 12px;
        .time {
          color: #999;
        }
      }
    }
  }
  .content {
    font-size: 16px;
    /deep/img {
      width: 100% !important;
    }
  }
  .comment {
    position: fixed;
    bottom: 0;
    left: 0;
    background-color: #fff;
    height: 70px;
    width: 100%;
    display: flex;
    z-index: 999;
    padding-top: 6px;
    align-items: center;
    .left {
      width: 50%;
      margin-left: 10px;
      margin-right: 15px;
      /deep/.van-field__control {
        background-color: #f7f4f5;
      }
    }
    .right {
      width: 40%;
      display: flex;
      color: #999;

      .item {
        flex: 1;
        text-align: center;
        font-size: 12px;
        /deep/.van-icon {
          font-size: 30px;
        }
      }
    }
  }
  .commentPop {
    .textarea {
      background-color: #f7f4f5;
      width: 330px;
      height: 120px;
      margin: 16px 20px 0 20px;
      font-size: 16px;
      padding: 10px;
      border: 0px;
      color: #999;
    }
    .senc {
      font-size: 16px;
      color: #999;
      text-align: right;
      margin-right: 20px;
    }
  }
  .commentarea {
    margin-top: 40px;
    .comtitle {
      font-size: 18px;
    }
  }
}
</style>
